package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.Publicity;
import cn.signalpha.modules.shop.entity.PublicityGoods;
import cn.signalpha.modules.shop.mapper.PublicityGoodsMapper;
import cn.signalpha.modules.shop.vo.PublicityGoodsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
@AllArgsConstructor
public class PublicityGoodsService extends ServiceImpl<PublicityGoodsMapper, PublicityGoods> {

	private final PublicityService publicityService;

	/**
	 * 自定义分页
	 */
    public IPage<PublicityGoodsVO> selectPublicityGoodsPage(IPage<PublicityGoodsVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectPublicityGoodsPage(page, params));
    }

	/**
	 * 根据推广id删除
	 */
	public void removeByPublicityId(Long publicityId) {
		baseMapper.delete(Wrappers.<PublicityGoods>lambdaQuery().eq(PublicityGoods::getPublicityId, publicityId));
	}

	/**
	 * 根据商品id删除
	 */
	public void removeByGoodsId(Long goodsId) {
		baseMapper.delete(Wrappers.<PublicityGoods>lambdaQuery().eq(PublicityGoods::getGoodsId, goodsId));
	}

	/**
	 * 是否已添加
	 */
	public boolean isExists(Long publicityId, Long goodsId) {
		return baseMapper.selectCount(Wrappers.<PublicityGoods>lambdaQuery().eq(PublicityGoods::getPublicityId, publicityId).eq(PublicityGoods::getGoodsId, goodsId)) > 0;
	}

	/**
	 * 根据商品id和类型搜搜
	 */
    public PublicityGoodsVO getByGoodsIdAndType(Long sellerId, Long goodsId, String publicityType) {
		List<Publicity> publicityList = publicityService.listBySellerIdAndType(sellerId, publicityType);
		for(Publicity publicity : publicityList){
			PublicityGoods publicityGoods = baseMapper.selectOne(Wrappers.<PublicityGoods>lambdaQuery()
				.eq(PublicityGoods::getGoodsId, goodsId).eq(PublicityGoods::getPublicityId, publicity.getId())
				.orderByDesc(PublicityGoods::getId).last("limit 1")
			);
			if(ObjectUtil.isNotEmpty(publicityGoods)){
				PublicityGoodsVO publicityGoodsVO = BeanUtil.copy(publicityGoods, PublicityGoodsVO.class);
				Objects.requireNonNull(publicityGoodsVO).setPublicityTitle(publicity.getTitle());
				Objects.requireNonNull(publicityGoodsVO).setPublicityConfig(publicity.getConfig());
				Objects.requireNonNull(publicityGoodsVO).setPublicityIsCoupon(publicity.getIsCoupon());
				return publicityGoodsVO;
			}
		}
		return null;
    }

	/**
	 * 根据商品id和类型搜搜
	 */
	public List<PublicityGoodsVO> listByGoodsId(Long goodsId) {
		Date date = new Date();
		List<PublicityGoods> publicityGoodsList = baseMapper.selectList(Wrappers.<PublicityGoods>lambdaQuery()
			.eq(PublicityGoods::getGoodsId, goodsId).le(PublicityGoods::getBeginTime, date).ge(PublicityGoods::getEndTime, date)
		);

		List<PublicityGoodsVO> publicityGoodsVOList = new ArrayList<>();
		publicityGoodsList.forEach(publicityGoods -> {
			Publicity publicity = publicityService.getById(publicityGoods.getPublicityId());
			if(ObjectUtil.isNotEmpty(publicity)){
				PublicityGoodsVO publicityGoodsVO = BeanUtil.copy(publicityGoods, PublicityGoodsVO.class);
				Objects.requireNonNull(publicityGoodsVO).setPublicityTitle(publicity.getTitle());
				Objects.requireNonNull(publicityGoodsVO).setPublicityConfig(publicity.getConfig());
				publicityGoodsVOList.add(publicityGoodsVO);
			}
		});
		return publicityGoodsVOList;
	}

}

